package com.feonix.common.view.verifycode;

import java.util.Date;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.feonix.common.util.NumberUtil;
import com.feonix.common.util.StringUtil;


public class VerifyCodeTag extends RequestContextAwareTag {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5852871593297002523L;
	
	/**
	 * 验证码的获取随机的key值
	 */
	private String randKey = "rand";

	/**
	 * 验证码宽度
	 */
	private String width = "110";

	/**
	 * 验证码高度
	 */
	private String height = "40";
	
	/**
	 * img的html的样式
	 */
	private	String style = "";
	
	/**
	 * 验证码的url
	 */
	private String url;

	protected int doStartTagInternal() throws Exception {
		StringBuffer stringBuffer = new StringBuffer(0);

		int width = NumberUtil.getInt(this.width, 110);
		int height = NumberUtil.getInt(this.height, 40);

		stringBuffer.append("<img id=\"verifyImg\" src=\"");
		stringBuffer.append(this.url);
		stringBuffer.append("?").append("width=").append(width).append("&height=").append(height);
		stringBuffer.append("&rand=").append(randKey);
		stringBuffer.append("&random=").append(new Date().getTime());
		stringBuffer.append("\"");
		stringBuffer.append(" onclick=\"this.src='");
		stringBuffer.append(this.url);
		stringBuffer.append("?").append("width=").append(width).append("&height=").append(height);
		stringBuffer.append("&rand=").append(randKey);
		stringBuffer.append("&random='+ Math.random();\"");
		stringBuffer.append(" style=\"cursor:pointer;vertical-align: middle;"+style+"\" ");
		stringBuffer.append(" width=\"");
		stringBuffer.append(width);
		stringBuffer.append("\" ");
		stringBuffer.append(" height=\"");
		stringBuffer.append(height);
		stringBuffer.append("\" ");
		stringBuffer.append("/>");
		try {
			this.pageContext.getOut().write(String.valueOf(stringBuffer.toString()));
			StringUtil.cleanStringBuffer(new StringBuffer[]{stringBuffer});
		} catch (Exception localException) {
			this.logger.error("模板输出失败", localException);
		}
		return 1;
	}
	
	public String getRandKey() {
		return randKey;
	}

	public void setRandKey(String randKey) {
		this.randKey = randKey;
	}

	public String getWidth() {
		return this.width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getHeight() {
		return this.height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getUrl() {
		return this.url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}
	
}
